<?php
/**
 * Created by : PhpStorm
 * User: Shane
 * Date: 2023-03-27
 * Time: 17:25
 */

namespace control;


class TimeMethod
{
    /**
     * 获取上周
     */
    public static function getLastWeek(){
        $startTime=date('Y-m-d H:i:s', strtotime('-1 monday', time()));
        $endTime=date('Y-m-d H:i:s',strtotime("$startTime +6 days"));
        return [$startTime,$endTime];
    }
    /**
     * 获取本周
     */
    public static function getThisWeek(){
        $startTime=date('Y-m-d H:i:s', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
        $endTime=date('Y-m-d H:i:s',strtotime("$startTime +6 days"));
        return [$startTime,$endTime];
    }

    /**
     * 获取下周
     */
    public static function getNextWeek(){
        $startTime=date('Y-m-d H:i:s', strtotime('+1 monday', time()));
        $endTime=date('Y-m-d H:i:s',strtotime("$startTime +6 days"));
        return [$startTime,$endTime];
    }
    /**
     * 获取上月
     */
    public static function getLastMonth(){
        $startTime= date('Y-m-d H:i:s', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00'))); //本月一日直接strtotime上减一个月
        $endTime=date('Y-m-d H:i:s', strtotime(date('Y-m', time()) . '-01 23:59:59') - 86400); //本月一日减一天即是上月最后一日
        return [$startTime,$endTime];
    }
    /**
     * 获取本月
     */
    public static function getThisMonth(){
        $startTime=date('Y-m-d H:i:s', strtotime(date('Y-m', time()) . '-01 00:00:00')); //直接以strtotime生成
        $endTime=date('Y-m-d H:i:s', strtotime(date('Y-m', time()) . '-' . date('t', time()) . ' 23:59:59')); //t为当月天数,28至31天
        return [$startTime,$endTime];
    }
    /**
     * 获取下月
     */
    public static function getNextMonth(){
        $startTime= date('Y-m-d H:i:s', strtotime('+1 month', strtotime(date('Y-m', time()) . '-01 00:00:00'))); //本月一日直接strtotime上减一个月
        $endTime=date('Y-m-d H:i:s', strtotime('+2 month', strtotime(date('Y-m', time()) . '-01 23:59:59'))- 86400); //本月一日减一天即是上月最后一日
        return [$startTime,$endTime];
    }
    /**
     * 获取上季度
     */
    public static function getLastQuarter(){
        $season = ceil((date('n'))/3)-1;//上季度是第几季度
        $startTime= date('Y-m-d H:i:s', mktime(0,0,0,$season*3-3+1,1,date('Y')));//2019-10-01 00:00:00
        $endTime= date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0,0,0,$season*3,1,date("Y"))),date('Y')));//2019-12-31 23:59:59
        return [$startTime,$endTime];
    }
    /**
     * 获取本季度
     */
    public static function getThisQuarter(){
        $season = ceil((date('n'))/3);
        $startTime= date('Y-m-d H:i:s', mktime(0,0,0,$season*3-3+1,1,date('Y')));//2020-01-01 00:00:00
        $endTime= date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0,0,0,$season*3,1,date("Y"))),date('Y'))); //2020-03-31 23:59:59
        return [$startTime,$endTime];
    }
    /**
     * 获取下季度
     */
    public static function getNextQuarter(){
        $season = ceil((date('n'))/3)+1;//上季度是第几季度
        $startTime= date('Y-m-d H:i:s', mktime(0,0,0,$season*3-3+1,1,date('Y')));//2019-10-01 00:00:00
        $endTime= date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0,0,0,$season*3,1,date("Y"))),date('Y')));//2019-12-31 23:59:59
        return [$startTime,$endTime];
    }
    /**
     * 获取去年
     */
    public static function getLastYear(){
        $lastYear=date("Y")-1;
        $startTime=date("{$lastYear}-01-01 00:00:00");
        $endTime = date("{$lastYear}-12-31 23:59:59");
        return [$startTime,$endTime];
    }
    /**
     * 获取今年
     */
    public static function getThisYear(){
        $startTime = date('Y-01-01 00:00:00');
        $endTime = date('Y-12-31 23:59:59');
        return [$startTime,$endTime];
    }
    /**
     * 获取明年
     */
    public static function getNextYear(){
        $lastYear=date("Y")+1;
        $startTime=date("{$lastYear}-01-01 00:00:00");
        $endTime = date("{$lastYear}-12-31 23:59:59");
        return [$startTime,$endTime];
    }
    /**
     * 获取指定时间月份的第一天
     * @param $date
     * @return false|string
     */
    public static function getCurMonthFirstDay($date)
    {
        return date('Y-m-01', strtotime($date));
    }

    /**
     * 获取指定时间月份的最后一天
     * @param $date
     * @return false|string
     */
    public static function getCurMonthLastDay($date)
    {
        return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' 1 month -1 day'));
    }

    /**
     * 返回时间：几秒前、几分钟前等........
     */
    public static function reckonTime($time)
    {
        $NowTime = time();
        $str = '';
        if($time > $NowTime){ return false; }
        $TimePoor = $NowTime - $time;
        if ($TimePoor < 60 && $TimePoor > 0) {
            $str = $TimePoor . '秒之前';
        } else if ($TimePoor >= 60 && $TimePoor <= 60 * 60) {
            $str = floor($TimePoor / 60) . '分钟前';
        } else if ($TimePoor > 60 * 60 && $TimePoor <= 3600 * 24) {
            $str = floor($TimePoor / 3600) . '小时前';
        } else if ($TimePoor > 3600 * 24 && $TimePoor <= 3600 * 24 * 7) {
            if (floor($TimePoor / (3600 * 24)) == 1) {
                $str = "昨天";
            } else if (floor($TimePoor / (3600 * 24)) == 2) {
                $str = "前天";
            } else {
                $str = floor($TimePoor / (3600 * 24)) . '天前';
            }
        } else if ($TimePoor > 3600 * 24 * 7) {
            $str = date("Y-m-d", $time);
        }
        return $str;
    }
}